
from rest_framework import  filters
from apps.common.models import Pagination
from apps.evaluation.filters import Evaluation_ListFilter, SmartLifeSupportCenter_BranchOfficeFilter, \
    DigitalIntelligence_Branch_officeFilter, SmartLifeSupportCenter_BusinessDepartmentFilter, \
    DigitalIntelligence_BusinessDepartmentFilter, Evaluation_SituationFilter, Evaluation_ManageFilter
from apps.evaluation.models import Evaluation_List, SmartLifeSupportCenter_BranchOffice, \
    DigitalIntelligence_Branch_office, SmartLifeSupportCenter_BusinessDepartment, DigitalIntelligence_BusinessDepartment
from apps.evaluation.serializers import Evaluation_ListSerializer, SmartLifeSupportCenter_BranchOfficeSerializer, \
    DigitalIntelligence_Branch_officeSerializer, DigitalIntelligence_BusinessDepartmentSerializer, \
    SmartLifeSupportCenter_BusinessDepartmentSerializer, Evaluation_SituationSerializer, Evaluation_ManageSerializer, \
    Evaluation_ResultSerializer
from apps.option.serializers import UserOptionSerializer
from apps.system.models import User
from extensions.permissions import IsAuthenticated
from extensions.viewsets import *


class Evaluation_ListViewSet(ModelViewSet, ExportMixin, ImportMixin):
    """参评列表"""
    # queryset = Evaluation_List.objects.all()
    serializer_class = Evaluation_ListSerializer
    pagination_class = Pagination
    permission_classes = [IsAuthenticated]
    filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
    filter_class = Evaluation_ListFilter
    search_fields = ['name']
    select_related_fields = ['evaluation', 'evaluation_authority']
    ordering_fields = ['id', 'name']

    def get_queryset(self):
        #获取用户的evaluation_authorities
        evaluation_authorities = self.request.user.evaluation_authorities
        #过滤Evaluation_List中evaluation_authority包含在evaluation_authorities中的数据
        queryset = Evaluation_List.objects.filter(evaluation_authority__in=evaluation_authorities)
        return queryset


class Evaluation_ManageViewSet(ModelViewSet, ExportMixin, ImportMixin):
    """考评管理"""
    serializer_class = Evaluation_ManageSerializer
    pagination_class = Pagination
    permission_classes = [IsAuthenticated]
    filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
    filter_class = Evaluation_ManageFilter
    search_fields = ['name']
    select_related_fields = ['evaluation', 'evaluation_authority']
    ordering_fields = ['id', 'name']
    queryset = Evaluation_List.objects.all()


class Evaluation_ResultViewSet(ModelViewSet, ExportMixin, ImportMixin):
    """考评结果"""
    # queryset = Evaluation_List.objects.all()
    serializer_class = Evaluation_ResultSerializer
    pagination_class = Pagination
    permission_classes = [IsAuthenticated]
    filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
    filter_class = Evaluation_ListFilter
    search_fields = ['name']
    select_related_fields = ['evaluation', 'evaluation_authority']
    ordering_fields = ['id', 'name']
    queryset = Evaluation_List.objects.all()

    # def get_queryset(self):
    #     #获取用户的evaluation_authorities
    #     evaluation_authorities = self.request.user.evaluation_authorities
    #     #过滤Evaluation_List中evaluation_authority包含在evaluation_authorities中的数据
    #     queryset = Evaluation_List.objects.filter(evaluation_authority__in=evaluation_authorities)
    #     return queryset



class SmartLifeSupportCenter_BranchOfficeViewSet(ModelViewSet, ExportMixin, ImportMixin):
    """智慧生活支撑中心(地市分公司评价)"""
    serializer_class = SmartLifeSupportCenter_BranchOfficeSerializer
    pagination_class = Pagination
    permission_classes = [IsAuthenticated]
    filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
    filter_class = SmartLifeSupportCenter_BranchOfficeFilter
    search_fields = ['name']
    select_related_fields = ['evaluation_item', 'reviewer']
    ordering_fields = ['id', 'name']
    queryset = SmartLifeSupportCenter_BranchOffice.objects.all()


class DigitalIntelligence_Branch_officeViewSet(ModelViewSet, ExportMixin, ImportMixin):
    """数智工程支撑中心(地市分公司评价)"""
    serializer_class = DigitalIntelligence_Branch_officeSerializer
    pagination_class = Pagination
    permission_classes = [IsAuthenticated]
    filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
    filter_class = DigitalIntelligence_Branch_officeFilter
    search_fields = ['name']
    select_related_fields = ['evaluation_item', 'reviewer']
    ordering_fields = ['id', 'name']
    queryset = DigitalIntelligence_Branch_office.objects.all()


class SmartLifeSupportCenter_BusinessDepartmentViewSet(ModelViewSet, ExportMixin, ImportMixin):
    """数智工程支撑中心(地市分公司评价)"""
    serializer_class = SmartLifeSupportCenter_BusinessDepartmentSerializer
    pagination_class = Pagination
    permission_classes = [IsAuthenticated]
    filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
    filter_class = SmartLifeSupportCenter_BusinessDepartmentFilter
    search_fields = ['name']
    select_related_fields = ['evaluation_item', 'reviewer']
    ordering_fields = ['id', 'name']
    queryset = SmartLifeSupportCenter_BusinessDepartment.objects.all()


class DigitalIntelligence_BusinessDepartmentViewSet(ModelViewSet, ExportMixin, ImportMixin):
    """数智工程支撑中心(地市分公司评价)"""
    serializer_class = DigitalIntelligence_BusinessDepartmentSerializer
    pagination_class = Pagination
    permission_classes = [IsAuthenticated]
    filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
    filter_class = DigitalIntelligence_BusinessDepartmentFilter
    search_fields = ['name']
    select_related_fields = ['evaluation_item', 'reviewer']
    ordering_fields = ['id', 'name']
    queryset = DigitalIntelligence_BusinessDepartment.objects.all()



class Evaluation_SituationViewSet(ModelViewSet, ExportMixin, ImportMixin):
    queryset = User.objects.all()
    serializer_class = Evaluation_SituationSerializer
    permission_classes = [IsAuthenticated]
    filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
    filter_class = Evaluation_SituationFilter
    # filterset_fields = ['is_active','company']
    search_fields = ['username', 'name']
    #
    # def get_queryset(self):
    #     queryset = User.objects.all()
    #
    #     # 获取查询参数has_reviewer的值
    #     has_reviewer = self.request.query_params.get('has_reviewer', None)
    #
    #     if has_reviewer is not None:
    #         # 如果has_reviewer参数存在，将其转换为布尔值
    #         has_reviewer_bool = has_reviewer.lower() == 'true'
    #
    #         # 使用filter_has_reviewer方法过滤查询集
    #         queryset = self.serializer_class.filter_has_reviewer(self, queryset, has_reviewer_bool)
    #
    #     return queryset
